import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { MemberSocialVo } from 'src/app/model/member-social-vo';
import { MemberVo } from 'src/app/model/member-vo';
import { BtnNaviSharedService } from 'src/app/services/btn-navi-shared.service';
import { MemberStorageService } from 'src/app/services/member-storage.service';
import { MemberService } from 'src/app/services/member.service';
import { UtilService } from 'src/app/services/util.service';

@Component({
  selector: 'app-personal-social',
  templateUrl: './personal-social.component.html',
  styles: [``]
})
export class PersonalSocialComponent implements OnInit {
  public curMember: MemberVo | null;
  public social: any = {
    member: 0,
    names: '',
    weibo: '',
    weixin: '',
    qq: '',
    mail: '',
    id: 0,
    token: '',
    ip: '127.0.0.1'
  };
  public errorMessage: string = '';

  constructor(
    private title: Title,
    private memberStorage: MemberStorageService,
    private memberService: MemberService,
    private util: UtilService,
    private btnNaviSharedService: BtnNaviSharedService) {
    this.curMember = this.memberStorage.get();
    this.btnNaviSharedService.active(false);//不需要全局的导航
    this.social.member = this.curMember?.id ?? 0;
    this.social.names = this.curMember?.names ?? 'guest';
    this.social.token = this.util.randomAlphaNumeric(8) || '123456';
    this.memberStorage.getIpCliente().subscribe(ipAddr=>this.social.ip=ipAddr);
  }
  
  //://stackoverflow.com/questions/37121134/how-to-deal-with-double-submit-in-angular2
  ngOnInit(): void {
    this.title.setTitle('个人中心:社交信息');
    this.memberService.getSocial(this.curMember?.id ?? 0).subscribe({
      next: response => {
        let ms: MemberSocialVo = response;
        this.social.weibo = ms.weibo;
        this.social.weixin = ms.weixin;
        this.social.qq = ms.qq;
        this.social.mail = ms.email;
        this.social.id = ms.id;
      },
      error: err => {
      },
      complete: () => {
        console.log('[PersonalSocial-CTL]Completed');
      }
    });
  }
  public checkSocial(submitBtn: HTMLButtonElement): void {
    submitBtn.disabled = true;
    submitBtn.setAttribute('value', '正在提交数据...');
    //
    if (this.social.id > 0) {
      this.updateSocial(submitBtn);
    } else {
      this.createSocial(submitBtn);
    }
  }
  private createSocial(submitBtn: HTMLButtonElement): void {
    this.memberService.createSocial(
      this.social.member,
      this.social.names,
      this.social.weibo,
      this.social.weixin,
      this.social.qq,
      this.social.mail,
      this.social.token, 
      this.social.ip).subscribe({
        next: response => {
          //跳转
          //this.router.navigate(['/member/home']);
          this.util.alertSuccess('社会化信息创建成功');
        },
        error: err => {
          if (err instanceof HttpErrorResponse) {
            if (err.error instanceof ErrorEvent) {
              this.errorMessage = err.error.message;
            } else {
              this.errorMessage = (err.status == 404) ? '社会化信息创建失败' : err.error;
            }
          } else if (err instanceof Error) {
            this.errorMessage = err.message;
          } else { this.errorMessage = err; }
        },
        complete: () => {
          console.log('[PersonalSocial-CTL]create social Completed');
          // 
          submitBtn.disabled = false;
          submitBtn.setAttribute('value', '开始更新');
        }
      });
  }
  private updateSocial(submitBtn: HTMLButtonElement): void {
    this.memberService.editSocial(
      this.social.member,
      this.social.names,
      this.social.id,
      this.social.weibo,
      this.social.weixin,
      this.social.qq,
      this.social.mail,
      this.social.token, 
      this.social.ip).subscribe({
        next: response => {
          //跳转
          //this.router.navigate(['/member/home']);
          this.util.alertSuccess('社会化信息更新成功');
        },
        error: err => {
          if (err instanceof HttpErrorResponse) {
            if (err.error instanceof ErrorEvent) {
              this.errorMessage = err.error.message;
            } else {
              this.errorMessage = (err.status == 417) ? '社会化信息更新失败' : err.error;
            }
          } else if (err instanceof Error) {
            this.errorMessage = err.message;
          } else { this.errorMessage = err; }
        },
        complete: () => {
          console.log('[PersonalSocial-CTL]edit social Completed');
          //
          submitBtn.disabled = false;
          submitBtn.setAttribute('value', '开始更新');
        }
      });
  }
}
